#include "DataChosenListDialog.h"
#include <QVBoxLayout>
#include <ElaCheckBox.h>
#include <ElaScrollArea.h>
#include <ElaText.h>
#include <ElaListView.h>
#include <QStandardItemModel>
#include <ElaScrollBar.h>
#include <ElaPushButton.h>

namespace pst
{
    DataChosenListDialog::DataChosenListDialog(QWidget* parent)
        : m_data()
        , _page1Layout(new QVBoxLayout(this))
        , _chooseAllCheckBox(new ElaCheckBox(this))
        , _listView(new ElaListView(this))
    {
        setWindowTitle("数据选择");

        this->setIsFixedSize(true);
        setContentsMargins(0, 0, 0, 0);
        this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
        setWindowModality(Qt::ApplicationModal);
        setWindowButtonFlags(ElaAppBarType::CloseButtonHint);

        _page1Layout->addStretch();
        _listView->setFixedHeight(200);
    }

    void DataChosenListDialog::setData(const QList<QString>& data)
    {
        m_data = data;
    }

    QList<QString> DataChosenListDialog::getData() const
    {
        return m_data;
    }

    void DataChosenListDialog::updateUI()
    {
        // 全选
        auto chooseAllText = new ElaText("全选", this);
        _chooseAllCheckBox->setTristate(true);
        auto chooseAllLayout = new QHBoxLayout();
        chooseAllLayout->addWidget(chooseAllText);
        chooseAllLayout->addWidget(_chooseAllCheckBox);
        chooseAllLayout->addStretch();
        _page1Layout->addLayout(chooseAllLayout);

        // 数据列表
        QStandardItemModel* model = new QStandardItemModel(_listView);
        for (const auto& i : m_data)
        {
            model->appendRow(new QStandardItem(i));
        }
        _listView->setModel(model);
        _listView->setSelectionMode(QAbstractItemView::SelectionMode::MultiSelection);
        ElaScrollBar* listViewFloatScrollBar = new ElaScrollBar(_listView->verticalScrollBar(), _listView);
        listViewFloatScrollBar->setIsAnimation(true);
        _page1Layout->addWidget(_listView);

        // 清除选择 完成
        ElaPushButton* clearButton = new ElaPushButton(this);
        clearButton->setText("清除选择");
        ElaPushButton* okButton = new ElaPushButton(this);
        okButton->setText("完成");
        auto clearOKLayout = new QHBoxLayout();
        clearOKLayout->addWidget(clearButton);
        clearOKLayout->addWidget(okButton);
        _page1Layout->addLayout(clearOKLayout);

        _page1Layout->addStretch();

        //全选
        connect(_chooseAllCheckBox, &QCheckBox::stateChanged, this,
            [this](int state)
            {
                if (state == Qt::CheckState::Checked)
                {
                    _listView->selectAll();
                }
                else if (state == Qt::CheckState::Unchecked)
                {
                    _listView->clearSelection();
                }
            }
        );

        /// 清除
        connect(clearButton, &QPushButton::clicked, this,
            [this]()
            {
                _chooseAllCheckBox->setChecked(false);
                _listView->clearSelection();
            });

        /// 选择的数据发生改变
        connect(_listView->selectionModel(), &QItemSelectionModel::selectionChanged,
            this, &DataChosenListDialog::slot_selectionChanged);

        connect(okButton, &QPushButton::clicked, this, &DataChosenListDialog::slot_okButtonClicked);
    }

    void DataChosenListDialog::setSelctedIndexs(const QList<int>& indexs)
    {
        auto model = _listView->selectionModel();
        for (const auto& i : indexs)
        {
            QModelIndex index = _listView->model()->index(i, 0);
            model->select(index, QItemSelectionModel::Select);
        }
    }

    void DataChosenListDialog::slot_okButtonClicked()
    {
        auto selectedIndexs = _listView->selectionModel()->selectedIndexes();
        emit signal_sentChosenDataIds(selectedIndexs);

        this->close();
    }

    void DataChosenListDialog::slot_selectionChanged(
        const QItemSelection& selected, const QItemSelection& deselected)
    {
        auto selectedCount = _listView->selectionModel()->selectedIndexes().count();
        if (selectedCount == 0)
        {
            _chooseAllCheckBox->setChecked(false);
        }
        else if (selectedCount == m_data.size())
        {
            _chooseAllCheckBox->setChecked(true);
        }
        else
        {
            _chooseAllCheckBox->setCheckState(Qt::CheckState::PartiallyChecked);
        }
    }
}